package com.solocredit.finance.ui.user.impl

import android.content.Context
import com.solocredit.finance.R
import com.solocredit.finance.base.utils.log.MullaLog
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.user.view.IMullaUserFeedbackView
import com.solocredit.finance.utils.okhttp.callback.StringCallback
import com.solocredit.finance.utils.okhttp.model.Response
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/16 16:26
 * @Annotation：意见反馈 Presenter 实现
 */
class MullaUserFeedbackPresenterImpl(context: Context?) : MvpPresenter<IMullaUserFeedbackView>(context) {

    private val typeTitleList = arrayListOf(R.string.feedback_type_suggest,R.string.feedback_type_bug,R.string.feedback_type_repayment_issues,R.string.feedback_type_other)
    private val typeValueList = arrayListOf("SUGGEST","BUG","PAYMENT","OTHER")

    //反馈类型列表
    private var typeList: ArrayList<JSONObject>? = null

    /**
     * 获取意见反馈类型
     */
    fun loadQuestionTypeListAction() {
        if (typeList == null){
            typeList = arrayListOf()
        }
        typeList?.clear()
        for (i in 0..<typeTitleList.size){
            val obj = JSONObject()
            obj.put("title", typeTitleList[i])
            obj.put("value", typeValueList[i])
            obj.put("isSelect",false)
            typeList?.add(obj)
        }
        typeList?.let {
            mvpView?.setSibQuestionTypeList(it)
        }
    }

    /**
     * 图片上传
     */
    fun uploadMullaImageAction(imgPath: String){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.MULLA_API_UPLOAD_FILE_URL)
        HttpHelper.uploadMullaFileAction(httpUrl,imgPath,object : StringCallback() {
            override fun onSuccess(response: Response<String>?) {
                response?.let { responseData ->
                    when(responseData.code()){
                        200 -> {
                            val data = JSONObject(responseData.body())
                            when(data.optInt("code")){
                                0 -> {//成功
                                    MullaLog.e("image upload success ：$data")
                                    val obj = data.optJSONObject("obj")
                                    obj?.let {
                                        val imgUrl = it.optString("url")
                                        mvpView?.showSibUploadImageSuccess(imgUrl)
                                    }
                                }
                                else -> {
                                    mvpView?.showMullaToast(data.optString("msg"))
                                }
                            }
                        }
                        else -> {
                            mvpView?.showMullaToast(responseData.message())
                        }
                    }
                }
            }
            override fun onError(response: Response<String>?) {
                super.onError(response)
                response?.let {
                    val errorMsg = it.message()
                    if (errorMsg != null){
                        mvpView?.showMullaToast(errorMsg)
                    }
                }
            }

            override fun onFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }

    /**
     * 提交反馈
     */
    fun submitMullaFeedbackAction(params: JSONObject){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.MULLA_API_FEEDBACK_SUBMIT_PATH)
        HttpHelper.postMullaAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val msg = data.optString("msg")
                mvpView?.showMullaToast(msg)
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView?.showSibSubmitSuccess()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }

    /**
     * 获取全部问题类型列表
     */
    fun getTypeList(): ArrayList<JSONObject>?{
        return typeList
    }
}